package com.xunqi.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import com.google.gson.Gson;

public class RequestUtil {

	/**
	 * 所有参数转换为map
	 * 
	 * @param request
	 * @return
	 */
	public static Map<String, Object> getParameterMap(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		Iterator<Entry<String, String[]>> entrys = request.getParameterMap().entrySet().iterator();
		Entry<String, String[]> entry;
		String key = "";
		while (entrys.hasNext()) {
			String value = "";
			entry = entrys.next();
			key = entry.getKey();
			String[] values = entry.getValue();
			for (String item : values) {
				value = item + "";
			}
			map.put(key, value);
		}
		return map;
	}

	/**
	 * 把所有参数转换为json字符串
	 * @param request
	 * @return
	 */
	public static String getAllParameterToString(HttpServletRequest request) {
		return new Gson().toJson(request.getParameterMap());
	}
}
